package ink.metoo.auto.fishpi.chatroom.handler.redpacket

import ink.metoo.auto.fishpi.Settings
import ink.metoo.auto.fishpi.chatroom.Message
import ink.metoo.auto.fishpi.chatroom.RedPacket

object RedPacketHandlerFactory : RedPacketHandler {

    override val types = RedPacket.Type.entries.toTypedArray()

    private val handlers = mutableListOf<RedPacketHandler>()

    init {
        handlers.add(SimpleRedPacketHandler())
        handlers.add(SpecifyRedPacketHandler())
        handlers.add(RockPaperScissorsRedPacketHandler())
    }

    override fun handle(message: Message, redPacket: RedPacket) {
        receivedStatusHandle(message, redPacket)
        if (!Settings.chatRoom.watchRedPacket) {
            return
        }
        if (redPacket.count <= redPacket.got) {
            // 红包已领完 就不凑热闹了
            return
        }
        handlers.forEach {
            if (it.types.contains(redPacket.typeEnum)) {
                it.handle(message, redPacket)
            }
        }
    }

    override fun receivedStatusHandle(message: Message, redPacket: RedPacket) {
        handlers.forEach {
            if (it.types.contains(redPacket.typeEnum)) {
                it.receivedStatusHandle(message, redPacket)
            }
        }
    }
}